package com.example.shanyi.zyfypt_204sy.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shanyi.zyfypt_204sy.R;
import com.example.shanyi.zyfypt_204sy.bean.LoginBean;
import com.example.shanyi.zyfypt_204sy.iface.LoginListener;
import com.example.shanyi.zyfypt_204sy.model.LoginModel;



public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
   private TextView tv1,tvname,tvwe,tvlogin,tv6;
    private EditText et1,et2;
    private Button btlogin,btreg;
    private ImageView imv;
    private CheckBox sw;
    private String username="",password="",sessionid="";//用户名，密码
    private SharedPreferences sp;

    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            System.out.println("result:"+loginBean.getId());
            if(loginBean.getSessionid()!=null) {
              username=loginBean.getUsername();
                sessionid=loginBean.getSessionid().toString();

                //rolename=loginBean.getRolename();
                System.out.println("--username="+username);

                System.out.println("----login sessionid="+sessionid);
                saveSP();
                Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent1);


            }
        }

        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toast.makeText(this,"登录",Toast.LENGTH_SHORT).show();
        init();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        //读取sp
        readSP();
    }
    //读取sp
    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
      String session=sp.getString("sessionid",null);

        Boolean b=sp.getBoolean("remember",false);
        if(b){
            et1.setText(name);
            et2.setText(pass);
            sw.setChecked(b);
        }
    }

    private void init()
    {
        //获取控件

        et1=(EditText)findViewById(R.id.Login_id);
        et2=(EditText)findViewById(R.id.Login_pwd);
        btlogin=(Button)findViewById(R.id.login);
        btreg=(Button)findViewById(R.id.register);

        sw = (CheckBox)findViewById(R.id.checkBox2);
        //设置按钮监听器
        btlogin.setOnClickListener(this);
        btreg.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                System.out.println("点击登陆按钮");
                username=et1.getText().toString();
                password=et2.getText().toString();
                if(username.equals(username)&&password.equals(password))
                {
                    saveSP();
                    LoginModel loginModel = new LoginModel();
                    loginModel.getResult(et1.getText().toString(), et2.getText().toString(), loginListener);

                }
                break;


            case R.id.register:
              // Intent intent1 = new Intent(LoginActivity.this, RegisterActivity.class);
               //startActivity(intent1);
                System.out.println("点击注册按钮");
                Intent intent = new Intent();
                intent.setClass(this, RegisterActivity.class);//forward为当前activity，forwardTarget为新的activity
                startActivity(intent);
                break;
        }
    }

    //保存sharedpreferences
    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putString("sessionid",sessionid);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();//提交保存
    }


}
